<template>
  <div class="goods">
    <breadcrumb :first="'商品管理'"></breadcrumb>
    <el-card>
      <el-input
        v-model="queryinfo.query"
        clearable
        placeholder="请输入内容"
      ></el-input>
      <el-button @click="search" type="primary" icon="el-icon-search"
        >搜索</el-button
      >
      <el-button @click="$router.push('/add')" type="primary">添加商品</el-button>

      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="630"
        ></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="100">
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button type="primary" size="mini">编辑</el-button>
            <el-button @click="delgoods(scope.row)" type="warning" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="changepagenum"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import breadcrumb from "@/components/breadcrumb.vue";
import { getGoodsList , delGoodsList} from "@/api/index";
export default {
  data() {
    return {
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 4,
      },
      goodsList: [],
      total: 0,
    };
  },
  components: {
    breadcrumb,
  },
  created() {
    this.getgoods();
  },
  methods: {
    getgoods() {
      getGoodsList(this.queryinfo).then((res) => {
        console.log(res.data);
        this.goodsList = res.data.data.goods;
        this.total = res.data.data.total;
      });
    },
    changepagenum(val) {
      this.queryinfo.pagenum = val;
      this.getgoods();
    },
    search() {
      this.getgoods();
    },
    delgoods(val){
        delGoodsList(val.goods_id).then( res=>{
            if(res.data.meta.status === 200){
                this.getgoods();
                this.$message({
                    message: '删除成功',
                    type:'success'
                })
            }
        })
    }
  },
};
</script>

<style lang="less" scoped>
.goods {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  .el-input {
    width: 200px;
  }
}
</style>
